/**
 * Date:2017年10月17日下午3:15:19
 *
 */

package com.example.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 
 * Description: 系统控制器,主要处理一些不需要登录就能操作的逻辑 <br/>
 * Date: 2018年10月11日 下午4:22:09 <br/>
 *
 * @author xuejianxinokok@163.com
 */
@Controller
@RequestMapping(value = "/")
public class AppController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AppController.class);

	@GetMapping(value = "hello")
	@ResponseBody
	@Tag(name = "测试hello方法")
	@Operation(security = { @SecurityRequirement(name = "bearer-key") })
	public String hello() throws UnknownHostException {
		InetAddress ia = InetAddress.getLocalHost();
		String host = ia.getHostName();// 获取计算机主机名
		String ip = ia.getHostAddress();// 获取计算机IP
		String msg = System.currentTimeMillis() + " test ok";
		log.info(msg);
		return String.format("host=%s\nip=%s\nmsg=%s\n", host, ip, msg);
	}

	/**
	 * 消息 TOPIC
	 */
	public static final String TOPIC = "testTOPIC";
	@Autowired(required = false)
	private KafkaTemplate<String, String> kafkaTemplate;

	@GetMapping(value = "kafka")
	@ResponseBody
	public Order sendMessage() {
		long orderId = System.currentTimeMillis();
		String orderNum = String.valueOf(orderId);
		LocalDateTime createTime = LocalDateTime.now();
		// 构建一个订单类
		Order order = new Order();
		order.setOrderId(orderId);
		order.setOrderNum(orderNum);
		order.setCreateTime(createTime);

		// 发送消息，订单类的 json 作为消息体
		ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(TOPIC, JSONObject.toJSONString(order));

		// 监听回调
		future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
			@Override
			public void onFailure(Throwable throwable) {
				log.info("## Send message fail ...");
			}

			@Override
			public void onSuccess(SendResult<String, String> result) {
				log.info("## Send message success ...");
			}
		});

		return order;
	}

	/**
	 * 
	 * @param file 文件
	 * @param ps 其他参数 
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> ps)
			throws FileNotFoundException, IOException {
		System.out.println(ps);
		var result = new HashMap<String, Object>();
		String fileName = file.getOriginalFilename();
		String path = "C:/Users/xue/Desktop/test/upload";
		FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(path, fileName)));
		result.put("success", true);
		result.put("message", "upload ok!!!");
		return result;
	}

}